/**
 * Copyright(c) by JNet Smartware Group.
 * All rights reserved. Not distribution or redistribution without
 * any permission of JNet Smartware Group.
 * 
 * @author thuc
 * 2010/07/31
 */
package vn.smartware.co.server.action;

import java.sql.Connection;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import vn.smartware.co.commons.Const;
import vn.smartware.co.dto.MstDrinkDTO;
import vn.smartware.co.dto.MstPriceDTO;
import vn.smartware.co.forms.RegisterDrinkForm;
import vn.smartware.co.forms.RegisterFoodForm;
import vn.smartware.co.framework.action.BaseAction;
import vn.smartware.co.framework.bean.LoginDTO;
import vn.smartware.co.framework.config.SystemConfig;
import vn.smartware.co.framework.exception.ServerException;
import vn.smartware.co.framework.form.ActionForm;
import vn.smartware.co.server.dao.factory.AbstractRegisterDrinkDAO;
import vn.smartware.co.server.dao.factory.AbstractRegisterFoodDAO;
import vn.smartware.co.server.dao.factory.DaoFactory;

/**
 * RegisterDrinkAction
 * @author thuctt
 */
@SuppressWarnings({"rawtypes"})
public class RegisterDrinkAction extends BaseAction {

	private static Log log = LogFactory.getLog(RegisterDrinkAction.class);
	
	/**
	 * init data
	 * @param request
	 * @param response
	 * @param config
	 * @param aForm
	 * @return ActionForm
	 * @throws ServerException
	 */
	public ActionForm getInitData(HttpServletRequest request,
			HttpServletResponse response, SystemConfig config, ActionForm aForm)
			throws ServerException {

		RegisterFoodForm form = (RegisterFoodForm)aForm;
		AbstractRegisterFoodDAO dao = DaoFactory.createRegisterFoodDAO();
		Connection conn = getConnection();
		
		try {
			
			LoginDTO user = (LoginDTO)form.getUserDTO();
			ArrayList list = dao.getUnitList(form, user, conn);
						
			form.setList(list);
			
		} catch (ServerException e) {
			log.error("ServerException.", e);
			throw e;
		} finally {
			closeConnection(conn);
		}
		return form;
	}
	
	/**
	 * get drink list
	 * @param request
	 * @param response
	 * @param config
	 * @param aForm
	 * @return ActionForm
	 * @throws ServerException
	 */
	public ActionForm getDrinkList(HttpServletRequest request,
			HttpServletResponse response, SystemConfig config, ActionForm aForm)
			throws ServerException {

		RegisterDrinkForm form = (RegisterDrinkForm)aForm;
		AbstractRegisterDrinkDAO dao = DaoFactory.createRegisterDrinkDAO();
		Connection conn = getConnection();
		
		try {
			
			LoginDTO user = (LoginDTO)form.getUserDTO();
			ArrayList list = dao.getDrinkList(form, user, conn);
						
			form.setList(list);
			
		} catch (ServerException e) {
			log.error("ServerException.", e);
			throw e;
		} finally {
			closeConnection(conn);
		}
		return form;
	}
	
	/**
	 * register food list
	 * @param request
	 * @param response
	 * @param config
	 * @param aForm
	 * @return ActionForm
	 * @throws ServerException
	 */
	public ActionForm saveAction(HttpServletRequest request,
			HttpServletResponse response, SystemConfig config, ActionForm aForm)
			throws ServerException {

		RegisterDrinkForm form = (RegisterDrinkForm)aForm;
		LoginDTO user = (LoginDTO)form.getUserDTO();
		AbstractRegisterDrinkDAO dao = DaoFactory.createRegisterDrinkDAO();
		Connection conn = getConnection();
		
		try {
			ArrayList ls = form.getList();
			for (int i = 0; i < ls.size(); i++) {
				MstDrinkDTO drink = (MstDrinkDTO)ls.get(i);
				if (Const.MODE_NEW.equals(drink.getProcessMode())){
					String drinkNo = dao.registerDrink(drink, user, conn);
					ArrayList priceLs = drink.getPriceList();
					for (int j = 0; j < priceLs.size(); j++) {
						MstPriceDTO price = (MstPriceDTO)priceLs.get(j);
						dao.registerPrice(drinkNo, price, user, conn);
					}
					
				} else if (Const.MODE_EDIT.equals(drink.getProcessMode())){
					dao.updateDrink(drink, user, conn);
					dao.deletePriceList(drink.getDrinkNo(), user,conn);
					ArrayList priceLs = drink.getPriceList();
					for (int j = 0; j < priceLs.size(); j++) {
						MstPriceDTO price = (MstPriceDTO)priceLs.get(j);
						dao.registerPrice(drink.getDrinkNo(), price, user, conn);
					}
					
				} else if (Const.MODE_DEL.equals(drink.getProcessMode())){
					dao.deleteDrink(drink.getDrinkNo(), user, conn);
					dao.deletePriceList(drink.getDrinkNo(), user,conn);
				}
			}
			
			commit(conn);
			
		} catch (ServerException e) {
			log.error("ServerException.", e);
			rollback(conn);
			throw e;
		} finally {
			closeConnection(conn);
		}
		return form;
	}
	
}
